package com.dao;

import java.sql.*;
import java.util.*;

import org.apache.catalina.tribes.transport.RxTaskPool;


public class ProductCategoryDaoImpl extends BaseDao<ProductCategory> implements ProductCategoryDao {


	@Override
	public String getTableName() {

		return "easybuy_product_category";

	}

	//按id查询商品
	@Override
	public List<ProductCategory> queryAllProductCategory(Integer parentId) {

		return getAll("where parentId = ?", parentId);

	}

	@Override
	public List<ProductCategory> getAllPro() {
		return getAll("");
	}

	@Override
	public List<ProductCategory> getAllLevelList() {
		// 查询所有数据，然后通过算法进行分级
		List<ProductCategory> all = this.getAllPro();
		System.out.println(this.getAllPro());
		// 用map来辅助分级
		Map<Integer, List<ProductCategory>> tmpMap = new HashMap<Integer, List<ProductCategory>>();

		for (ProductCategory pc : all) {
			// 使用parentId作为分组条件
			Integer parentId = pc.getParentId();
			// 先获取集合
			List<ProductCategory> list = tmpMap.get(parentId);
			if (list == null) {
				// 没有数据，就创建一个
				list = new ArrayList<>();
				tmpMap.put(parentId, list);
			}
			// 把数据加入到集合
			list.add(pc);
		}
		// 第一组数据
		List<ProductCategory> tt = tmpMap.get(0);
		cascade(tt, tmpMap);

		return tt;
	}

	public void cascade(List<ProductCategory> root, Map<Integer, List<ProductCategory>> tmpMap) {
		for (ProductCategory pc : root) {
			// 获取一级iD
			Integer id = pc.getId();
			// 把id当成key
			List<ProductCategory> children = tmpMap.get(id);
			if (children == null) {
				// 跳过
				continue;
			}
			// 设置子级
			pc.setChildren(children);
			// 递归
			cascade(children, tmpMap);
		}
	}

	@Override
	public String getProByParentId(Integer id, Integer parentId) {
		String sql = "select * from easybuy_product_category";
		ResultSet rs = null;
		List<Object> list = new ArrayList<Object>();
		if (parentId == 0) {
			sql += " where parentId = ? and id = ?";
			list.add(parentId);
			list.add(id);
		} else {
			sql += " where id = ?";
			list.add(parentId);
		}
		String pro = null;
		try {

			rs = executeQuery(sql, list.toArray());
			while (rs.next()) {
				pro = rs.getString("name");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(null, null, rs);
		}
		return pro;
	}

	@Override
	public int delProById(int id) {
		// TODO Auto-generated method stub
		try {
			return executeUpdate("delete from easybuy_product_category where id = ?", id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<ProductCategory> getProById(int id, int type) {
		ProductCategory pro = null;
		ResultSet rs = null;
		String sql = "";
		List<Object> pa = new ArrayList<Object>();
		List<ProductCategory> list = new ArrayList<ProductCategory>();
		//级别位1的情况，需删除它本身和它下面的子集
		if (type == 1) {
			sql = "select * from easybuy_product_category where parentId in("
					+ "	select id from easybuy_product_category where parentId = ?" + ") or id in ("
					+ "	select id from easybuy_product_category where parentId = ?" + ")or id = ?";
			pa.add(id);
			pa.add(id);
			pa.add(id);
		}
		//级别位2的话就也要它本身和它下面的子集
		if (type == 2) {
			sql = "select * from easybuy_product_category where id in("
					+ "	select id from easybuy_product_category where parentId = ?" + ") or id = ?";
			pa.add(id);
			pa.add(id);
		}
		//级别位3的话只需删除本身即可
		if (type == 3) {
			sql = "select * from easybuy_product_category where id = ?";
			pa.add(id);
		}
		try {
			rs = executeQuery(sql, pa.toArray());

			while (rs.next()) {
				pro = new ProductCategory();
				pro.setId(rs.getInt("id"));
				pro.setName(rs.getString("name"));
				pro.setParentId(rs.getInt("parentId"));
				pro.setType(rs.getInt("type"));
				list.add(pro);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(null, null, rs);
		}
		return list;
	}

	@Override
	public List<ProductCategory> getPagePro(Integer pageNo, Integer pageSize) {
		if(pageNo != null && pageSize != null) {
			return getAll(" limit ?,?", (pageNo - 1) * pageSize,pageSize);
		}
		return getAll("");
	}

	@Override
	public int getTotalCount() {
		String sql = "select count(*) from " + getTableName();
		ResultSet rs = null;
		int num = 0;
		try {
			rs = executeQuery(sql);
			
			while(rs.next()) {
				num = rs.getInt(1);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			closeAll(null, null, rs);
		}
		return num;
	}

	@Override
	public int addCategory(ProductCategory pc) {
		// TODO Auto-generated method stub
		try {
			return executeUpdate("INSERT INTO easybuy_product_category (`name`, `parentId`, `type`) VALUES (?,?,?)", pc.getName(),pc.getParentId(),pc.getType());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updateCategory(ProductCategory pc) {
		// TODO Auto-generated method stub
		try {
			return executeUpdate("UPDATE easybuy_product_category SET `name`=?, `parentId`=?, `type`=? WHERE (`id`=?);"
					,pc.getName(),pc.getParentId(),pc.getType(),pc.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public ProductCategory getCategoryById(int id) {
		String sql = "select * from easybuy_product_category where id = ?";
		ResultSet rs = null;
		ProductCategory pc = null;
		try {
			
			rs = executeQuery(sql, id);
			
			while(rs.next()) {
				pc = new ProductCategory();
				pc.setId(rs.getInt("id"));
				pc.setName(rs.getString("name"));
				pc.setParentId(rs.getInt("parentId"));
				pc.setType(rs.getInt("type"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeAll(null, null, rs);
		}
		return pc;
	}

}
